
package progentiere.knapsackprb;

import java.util.Collections;


public class GreedyHeuristic extends Algorithm
{
    @Override
    Solution solve(KnapSackProblem problem) 
    {
        Collections.sort(problem.getItems());
        
        Solution solution = new Solution(problem.getNbItems());
        double remainingCapacity = problem.getCapacity();
        
        for(int i = 0 ; i < problem.getNbItems() ; i++)
        {
            if(problem.getItems().get(i).getWeight() <= remainingCapacity)
            {
                solution.getContent()[i] = 1;
                remainingCapacity -= problem.getItems().get(i).getWeight();
            }
            else 
            {
                solution.getContent()[i] = 0;
            }
        }
        
        return solution;
    }
}
